package com.xb.service;

import com.xb.dao.ArticleDao;
import com.xb.entity.Article;
import com.xb.entity.User;
import com.xb.utils.PageEntity;

import java.util.List;

public class ArticleService {
    private ArticleDao articleDao=new ArticleDao();


    /**
     * 文章搜索+分页
     * @param title
     * @param currPage
     * @return
     */
    public PageEntity<Article> findPage(String title, Integer currPage) {
        PageEntity<Article> pageData=new PageEntity<>();
        //页大小
        Integer pageSize=pageData.getPageSize();

        //当前页的数据
        Integer startIndex=(currPage-1)*pageSize;
        List<Article> articleList=articleDao.findPage(title,startIndex,pageSize);


        //总记录数
        Integer totalCount= articleDao.countByTitle(title);

        //总页数
        Integer totalPage =totalCount % pageSize==0? totalCount / pageSize:(totalCount / pageSize)+1;

        pageData.setCurrPage(currPage);
        pageData.setData(articleList);
        pageData.setTotalPage(totalPage);
        pageData.setTotalCount(totalCount);

        return pageData;
    }


    /**
     * 发布文章
     * @param article
     */
    public void save(Article article) {
        articleDao.save(article);
    }


    /**
     * 我的收藏+搜索+分页
     *
     * @param title
     * @param currPage
     * @param userId
     * @return
     */
    public PageEntity<Article> findFavoritePage(String title, Long userId, Integer currPage) {
        PageEntity<Article> pageData = new PageEntity<>();

        // 页大小
        Integer pageSize = pageData.getPageSize();

        // 1. 当前页的数据
        Integer startIndex = (currPage - 1) * pageSize;
        List<Article> articleList = articleDao.findFavoritePage(title, userId, startIndex, pageSize);

        // 2. 总记录数
        Integer totalCount = articleDao.countFavoirtePage(title, userId);

        // 3. 总页数
        Integer totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;

        pageData.setCurrPage(currPage);
        pageData.setData(articleList);
        pageData.setTotalCount(totalCount);
        pageData.setTotalPage(totalPage);

        return pageData;
    }


    /**
     * 更新浏览量
     * @param articleId
     */
    public void updateBrowseCount(Long articleId) {
        articleDao.updateBrowseCount(articleId);
    }


    /**
     * 文章详情
     * @param articleId
     * @return
     */
    public Article findById(Long articleId) {
        return articleDao.findById(articleId);
    }


    /**
     * 收藏文章次数
     * @param articleId
     * @return
     */
    public Integer findFavoriteCount(Long articleId) {
        return articleDao.findFavoriteCount(articleId);
    }


    /**
     * 我是否收藏过该文章
     * @param id
     * @param articleId
     * @return
     */
    public Boolean findFavorite(Long id, Long articleId) {
        return articleDao.findFavoriteId(id,articleId)>0;
    }


    /**
     * 我关注的人，并且他们也收藏了这篇文章
     * @param id
     * @param articleId
     * @return
     */
    public List<User> findUserList(Long id, Long articleId) {
        return articleDao.findUserList(id,articleId);
    }


    /**
     * 我是来取消文章的
     * @param id
     * @param articleId
     */
    public void delFavorite(Long id, Long articleId) {
        articleDao.delFavorite(id,articleId);
    }


    /**
     * 我是来收藏文章的
     * @param id
     * @param articleId
     */
    public void addFavorite(Long id, Long articleId) {
        articleDao.addFavorite(id,articleId);

    }
}
